package com.canbot.u05.activity.presidemode.newcode.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.canbot.u05.R;
import com.canbot.u05.activity.presidemode.newcode.bean.DanceBean;
import com.canbot.u05.activity.presidemode.newcode.interfaces.IItemClick;
import com.canbot.u05.socket.IpMsgConst;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class NChoiceDanceAdapter extends RecyclerView.Adapter<NChoiceDanceAdapter.NChoiceDanceViewHolder> {
    private final Context mContext;
    private List<DanceBean> mTaskList;
    private LayoutInflater layoutInflater;
    private IItemClick mIItemClick;
    private int mPrevCheckPos = -1;

    public NChoiceDanceAdapter(List<DanceBean> taskList, Context context, IItemClick iItemClick) {
        this.mTaskList = taskList;
        if (mTaskList == null) {
            mTaskList = new ArrayList<>();
        }
        this.mContext = context;
        layoutInflater = LayoutInflater.from(mContext);
        this.mIItemClick = iItemClick;
    }

    @NonNull
    @Override
    public NChoiceDanceViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        View view = layoutInflater.inflate(R.layout.item_choice_dance, null, false);
        return new NChoiceDanceViewHolder(view);
    }

    private static final String TAG = "NChoiceMusicAdapter";

    @Override
    public void onBindViewHolder(@NonNull NChoiceDanceViewHolder viewHolder, final int postion) {
        final DanceBean danceBean = mTaskList.get(postion);
        viewHolder.danceName.setText(danceBean.getContent());
        //viewHolder.ivImage.setImageBitmap(BitmapFactory.decodeResource(mContext.getResources(), danceBean.getResBkId()));
        Glide.with(mContext)
                .asDrawable()
                .load(IpMsgConst.HTTP_URL_CHEST + danceBean.getImageUrl())
                .thumbnail(0.1f)
                .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存策略
                .skipMemoryCache(false)
                .placeholder(R.drawable.sales_banner01)//占位图
                .error(R.drawable.ic_picture_loadfailed)
                //.override(100, 100)
                .into(viewHolder.ivImage);
        if (danceBean.isSelect()) {
            viewHolder.ivCheck.setVisibility(View.VISIBLE);
        } else {
            viewHolder.ivCheck.setVisibility(View.INVISIBLE);
        }
        viewHolder.itemGridView.setOnClickListener(v -> {
            if (mIItemClick != null) {
                mIItemClick.onClick(postion, v);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mTaskList.size();
    }

    public class NChoiceDanceViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.dance_name)
        TextView danceName;
        @BindView(R.id.iv_check)
        ImageView ivCheck;
        @BindView(R.id.item_grid_view)
        RelativeLayout itemGridView;
        @BindView(R.id.iv_image)
        ImageView ivImage;

        public NChoiceDanceViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
